//
//  NSError+HttpRequest.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/3.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "NSError+HttpRequest.h"

@implementation NSError (HttpRequest)


/**
 处理AF框架请求返回的错误，非服务器返回错误
 NSURLErrorUnknown =             -1,
 NSURLErrorCancelled =             -999,
 NSURLErrorBadURL =                 -1000,
 NSURLErrorTimedOut =             -1001,
 NSURLErrorUnsupportedURL =             -1002,
 NSURLErrorCannotFindHost =             -1003,
 NSURLErrorCannotConnectToHost =         -1004,
 NSURLErrorNetworkConnectionLost =         -1005,
 NSURLErrorDNSLookupFailed =         -1006,
 NSURLErrorHTTPTooManyRedirects =         -1007,
 NSURLErrorResourceUnavailable =         -1008,
 NSURLErrorNotConnectedToInternet =         -1009,
 NSURLErrorRedirectToNonExistentLocation =     -1010,
 NSURLErrorBadServerResponse =         -1011,
 NSURLErrorUserCancelledAuthentication =     -1012,
 NSURLErrorUserAuthenticationRequired =     -1013,
 NSURLErrorZeroByteResource =         -1014,
 NSURLErrorCannotDecodeRawData =             -1015,
 NSURLErrorCannotDecodeContentData =         -1016,
 NSURLErrorCannotParseResponse =             -1017,
 NSURLErrorAppTransportSecurityRequiresSecureConnection API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0)) = -1022,
 NSURLErrorFileDoesNotExist =         -1100,
 NSURLErrorFileIsDirectory =         -1101,
 NSURLErrorNoPermissionsToReadFile =     -1102,
 NSURLErrorDataLengthExceedsMaximum API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) =    -1103,
 NSURLErrorFileOutsideSafeArea API_AVAILABLE(macos(10.12.4), ios(10.3), watchos(3.2), tvos(10.2)) = -1104,
 
 // SSL errors
 NSURLErrorSecureConnectionFailed =         -1200,
 NSURLErrorServerCertificateHasBadDate =     -1201,
 NSURLErrorServerCertificateUntrusted =     -1202,
 NSURLErrorServerCertificateHasUnknownRoot = -1203,
 NSURLErrorServerCertificateNotYetValid =     -1204,
 NSURLErrorClientCertificateRejected =     -1205,
 NSURLErrorClientCertificateRequired =    -1206,
 NSURLErrorCannotLoadFromNetwork =         -2000,
 
 // Download and file I/O errors
 NSURLErrorCannotCreateFile =         -3000,
 NSURLErrorCannotOpenFile =             -3001,
 NSURLErrorCannotCloseFile =         -3002,
 NSURLErrorCannotWriteToFile =         -3003,
 NSURLErrorCannotRemoveFile =         -3004,
 NSURLErrorCannotMoveFile =             -3005,
 NSURLErrorDownloadDecodingFailedMidStream = -3006,
 NSURLErrorDownloadDecodingFailedToComplete =-3007,
 
 NSURLErrorInternationalRoamingOff API_AVAILABLE(macos(10.7), ios(3.0), watchos(2.0), tvos(9.0)) =         -1018,
 NSURLErrorCallIsActive API_AVAILABLE(macos(10.7), ios(3.0), watchos(2.0), tvos(9.0)) =                    -1019,
 NSURLErrorDataNotAllowed API_AVAILABLE(macos(10.7), ios(3.0), watchos(2.0), tvos(9.0)) =                  -1020,
 NSURLErrorRequestBodyStreamExhausted API_AVAILABLE(macos(10.7), ios(3.0), watchos(2.0), tvos(9.0)) =      -1021,
 
 NSURLErrorBackgroundSessionRequiresSharedContainer API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)) = -995,
 NSURLErrorBackgroundSessionInUseByAnotherProcess API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0)) = -996,
 NSURLErrorBackgroundSessionWasDisconnected API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0))= -997,
 */
+ (NSError *)handleAFErrorWithDataTask:(NSURLSessionDataTask *)dataTask error:(NSError *)error {
    NSError *afError;
    if (!error) {
        return [NSError errorWithDomain:@"Unkown Error" code:0 userInfo:nil];
    }
    NSInteger errorCode = error.code;
    NSString *domain;
    switch (errorCode) {
        case NSURLErrorTimedOut://请求超时
        {
            domain = BCLocalizedKey(@"Request Time Out");
            afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
        }
            break;
        case NSURLErrorCannotConnectToHost://不能连接到服务器
        {
            domain = BCLocalizedKey(@"Can not connect to Host");
            afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
        }
            break;
        case NSURLErrorNetworkConnectionLost://网络问题
        case NSURLErrorNotConnectedToInternet:
        {
            domain = BCLocalizedKey(@"The network connection lost,Please check");
            afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
        }
            break;
        default://统一处理为请求失败：+状态码
        {
            //判断一下http状态码 500这些是服务器发生错误 Request failed: internal server error (500)
            if (dataTask) {
                NSURLResponse *response = dataTask.response;
                NSHTTPURLResponse *httpResp;
                if ([response isKindOfClass:NSHTTPURLResponse.class]) {
                    httpResp = (NSHTTPURLResponse *)response;
                    if (httpResp.statusCode==500) {
                        domain = BCLocalizedFormatString(@"Request failed: internal server error (%@)",@(httpResp.statusCode));
                        afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
                    }
                }
                if (!afError) {
                    //                domain = BCLocalizedFormatString(@"Request failed: %@",@(errorCode));
                    domain = BCLocalizedFormatString(@"Request failed:errorCode=%@,statusCode=%@",@(errorCode),@(httpResp.statusCode));
                    afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
                }
            }else {
                domain = BCLocalizedFormatString(@"Request failed: %@",@(errorCode));
                afError = [NSError errorWithDomain:domain code:errorCode userInfo:nil];
            }
        }
            break;
    }
    
    return afError;
}

@end
